package com.qf.conf;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    //交换机对象
    @Bean
    public Exchange topicExchange(){
        return ExchangeBuilder.topicExchange("spTopicEX").durable(true).build();
    }

    //队列对象
    @Bean
    public Queue topicQueue1(){
        return QueueBuilder.durable("spTopicQue1").build();
    }
    @Bean
    public Queue topicQueue2(){
        return QueueBuilder.durable("spTopicQue2").build();
    }

    //交换机和队列的绑定关系对象
    @Bean
    public Binding bindingQue1(Queue topicQueue1,Exchange topicExchange){
        return BindingBuilder.bind(topicQueue1).to(topicExchange).with("spA.*").noargs();
    }
    @Bean
    public Binding bindingQue2(Queue topicQueue2,Exchange topicExchange){
        return BindingBuilder.bind(topicQueue2).to(topicExchange).with("spA.#").noargs();
    }

    /*****************************************************/
    @Bean
    public Exchange directEx(){
        return ExchangeBuilder.directExchange("directEx").build();
    }

    @Bean
    public Queue directQue(){
        return QueueBuilder.durable("directQue")
//                .deadLetterExchange("dlEx")
//                .deadLetterRoutingKey("dlrKey")
//                //消息队列的最大长度
//                .maxLength(5)
//                //设置队列中消息的超时时间
//                .ttl(10000)
                //设置该队列为惰性队列（消息以磁盘方式存储，不是内存方式存储）
                .lazy()
                .build();
    }

    //绑定普通队列和普通交换机的关系
    @Bean
    public Binding binding1(Queue directQue,Exchange directEx){
        return BindingBuilder.bind(directQue).to(directEx).with("rkey").noargs();
    }

    //创建死信交换机
    @Bean
    public Exchange dlEx(){
        return ExchangeBuilder.directExchange("dlEx").build();
    }
    //创建死信队列
    @Bean
    public Queue dlQue(){
        return QueueBuilder.durable("dlQue")
                .build();
    }

    //绑定死信队列和死信交换机的关系
    @Bean
    public Binding binding2(Queue dlQue,Exchange dlEx){
        return BindingBuilder.bind(dlQue).to(dlEx).with("dlrKey").noargs();
    }


}
